package com.ITArmy.controller;


import com.ITArmy.beans.Blog;
import com.ITArmy.beans.Comment;
import com.ITArmy.beans.Shows.CommentMessage;
import com.ITArmy.beans.Shows.ReadBlog;
import com.ITArmy.beans.Type;
import com.ITArmy.beans.User;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.controller.base.BaseController;
import com.ITArmy.dao.TypeMapper;
import com.ITArmy.dao.UserMapper;
import com.ITArmy.service.BlogService;
import com.ITArmy.service.TypeService;
import com.ITArmy.service.UserService;
import com.ITArmy.util.GetUserSession;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;


/**
 * @author Administrator
 */
@Api(value = "博客接口层次", tags = "博客接口层次Controller层")
@Controller
@RequestMapping("/blog")
public class BlogController extends BaseController {
    GetUserSession getUserSession = new GetUserSession();
    @Autowired
    UserMapper userMapper;
    @Autowired
    TypeMapper typeMapper;

    @RequestMapping("/findBlogByTitle")
    @ApiOperation(value = "根据标题模糊查询，",
            httpMethod = "POST", response = ServerResponse.class)
    public String findBlogByTitle(String title, Model model) {
        ServerResponse blogServerResponse = blogService.findBlogByTitle(title);
        model.addAttribute("blogListByTitle", blogServerResponse.getData());
        return "blog";
    }

    @RequestMapping("/findBlogByTypeId")
    @ApiOperation(value = "根据博客类型搜索(前台)", notes = "类型用固定选择",
            httpMethod = "GET")
    public String findBlogByTypeId(Integer tid, Model model) {
        //遍历所有的类型
        ServerResponse typeServerResponse = typeService.typeList();
        List<Type> typeList = (List<Type>) typeServerResponse.getData();
        String type = "Java";
        try {
            type = typeMapper.selectByPrimaryKey((long) tid).gettName();
        } catch (NullPointerException nu) {
            System.out.println("...");
        }
        model.addAttribute("allBlogType", typeServerResponse.getData());
        model.addAttribute("blogs", blogService.findBlogByType(tid));
        model.addAttribute("type", type);
        return "blogType";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除博客信息，", notes = "单个删除",
            httpMethod = "DELETE", response = ServerResponse.class)
    public ServerResponse delete(Long id) {
        System.out.println("删除的博客的id为：" + id);
        return blogService.deleteBlogById(id);
    }

    @RequestMapping("/recommendList")
    @ResponseBody
    @ApiOperation(value = "遍历推荐博客",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse recommendList() {
        return blogService.recommendList();
    }

    @RequestMapping("/findRecommendByType")
    @ResponseBody
    @ApiOperation(value = "根据博客类型遍历推荐博客",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse findRecommendByType(Integer tid) {
        return blogService.findRecommendByType(tid);
    }

    @RequestMapping("/findLikeBlog")
    @ApiOperation(value = "根据用户id遍历用户喜欢的博客*",
            httpMethod = "POST", response = String.class)
    public String findLikeBlog(HttpServletRequest request, Model model) {
        getUserSession.addUserModel(request, model);
        model.addAttribute("likeBlogs", blogService.findLikeBlog(request));
        return "index";
    }

    @RequestMapping("/publicshBlog")
    @ApiOperation(value = "根据用户id显示用户发表的所有博客*",
            httpMethod = "POST", response = String.class)
    public String publicshBlog(HttpServletRequest request, Model model) {
        getUserSession.addUserModel(request, model);
        model.addAttribute("publicshBlog", blogService.getPublishCount(request).getData());
        return "index";
    }

    @RequestMapping("/addBlog")
    @ResponseBody
    @ApiOperation(value = "用户发表博客", httpMethod = "POST")
    public ServerResponse addBlog(HttpServletRequest request, Blog blog) {
        System.out.println("我运行了...");
        System.out.println(blog.gettId() + "博客类型为...");
        System.out.println(blog.getbTitle());
        System.out.println(blog.getbAddress());
        System.out.println("字数为" + blog.getbWord());
        return blogService.addBlog(request, blog);
    }

    @RequestMapping("/readBlog")
    @ApiOperation(value = "用户看博客", httpMethod = "GET", response = String.class)
    public String readBlog(long blogId, Model model) {
        model.addAttribute("blog", blogService.findBlogById(blogId, request));
        //查询对博客的评论(全部)Collections.reverse(list);
        List<CommentMessage> commentMessages = blogService.getBlogCommentMessage(blogId);
        Collections.reverse(commentMessages);
        model.addAttribute("commentMessage", commentMessages);
        return "blogArticle";
    }

    //进行延时加载博客，从第二页开始加载
    @RequestMapping("/nextPage")
    @ResponseBody
    @ApiOperation(value = "进行展示",
            httpMethod = "POST", response = Object.class)
    public Object list(@RequestParam(value = "page", defaultValue = "1") int page) {
        PageHelper.startPage(page, 6);
        List<ReadBlog> blogs=blogService.getAllReadBlogs(request);
        PageInfo blogPageInfo = new PageInfo<>(blogs, 6);
        return blogPageInfo;
    }

}
